package com.hbnu.net;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * @author kittychen
 * @since 2025-10-28 19:19
 */
public class UdpSendDemo02 {

    public static void main(String[] args) {
        DatagramSocket datagramSocket = null;

        try {
            datagramSocket = new DatagramSocket();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            String line;

            while ((line = bufferedReader.readLine()) != null) {  // readLine方法是一个阻塞式方法
                byte[] bytes = line.getBytes();
                DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length,
                        InetAddress.getByName("127.0.0.1"), 8888);
                datagramSocket.send(datagramPacket);
                if ("886".equals(line)) {
                    break;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (datagramSocket != null)
                datagramSocket.close();
        }
    }
}
